package org.csu.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.ResponseCode;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Account;
import org.csu.mall.entity.CommodityRate;
import org.csu.mall.entity.CustomerOrder;
import org.csu.mall.persistence.AccountMapper;
import org.csu.mall.persistence.CommodityRateMapper;
import org.csu.mall.persistence.CorderMapper;
import org.csu.mall.service.ICommodityRateService;
import org.csu.mall.util.DateUtil;
import org.csu.mall.vo.CommodityRateListVO;
import org.csu.mall.vo.CommodityRateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service("commodityRateService")
public class ICommodityRateServiceImpl implements ICommodityRateService {

    @Autowired
    private CommodityRateMapper commodityRateMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private CorderMapper corderMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public UniformResponse<String> reviewCommodity(CustomerOrder customerOrder, Account account) {
        if(account == null){
            return UniformResponse.createForErrorCodeMessage(
                    ResponseCode.USER_NOT_LOGIN.getCode(), ResponseCode.USER_NOT_LOGIN.getDescription());
        }
        //更新订单评价
        customerOrder.setUpdateTime(LocalDateTime.now());
        customerOrder.setOrderState(CONSTANT.CustomerOrderStatus.EVALUATION_ORDER.getCode());
        corderMapper.updateById(customerOrder);
        //插入商品评价
        CommodityRate commodityRate = new CommodityRate();
        commodityRate.setUserId(account.getUserId());
        commodityRate.setItemId(customerOrder.getItemId());
        commodityRate.setItemName(customerOrder.getItemName());
        commodityRate.setItemStar(customerOrder.getOrderStar());
        commodityRate.setComment(customerOrder.getOrderReview());
        commodityRate.setCreateTime(LocalDateTime.now());
        commodityRate.setUpdateTime(LocalDateTime.now());
        int row = commodityRateMapper.insert(commodityRate);
        if(row > 0){
            return UniformResponse.createForSuccessMessage(ResponseCode.SUCCESS.getDescription());
        }
        return UniformResponse.createForErrorCodeMessage(
                ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDescription());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public UniformResponse<CommodityRateListVO> commodityComments(String itemId, int pageSize, int pageNum) {
        Page<CommodityRate> result = new Page<>();
        result.setCurrent(pageNum);
        result.setSize(pageSize);

        QueryWrapper<CommodityRate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", itemId);

        result = commodityRateMapper.selectPage(result, queryWrapper);
        CommodityRateListVO commodityRateListVO = this.pageToVO(result);
        return UniformResponse.createForSuccess(ResponseCode.SUCCESS.getDescription(), commodityRateListVO);
    }

    private CommodityRateVO entityToVO(CommodityRate commodityRate){
        CommodityRateVO commodityRateVO = new CommodityRateVO();
        commodityRateVO.setId(commodityRate.getUserId());
        commodityRateVO.setItemId(commodityRate.getItemId());
        commodityRateVO.setItemName(commodityRate.getItemName());
        commodityRateVO.setItemStar(commodityRate.getItemStar());
        commodityRateVO.setComment(commodityRate.getComment());
        commodityRateVO.setUserId(commodityRate.getUserId());

        //新增属性
        String username = accountMapper.selectOne(
                Wrappers.<Account>query().select("username").eq("user_id", commodityRate.getUserId())).getUsername();
        commodityRateVO.setUsername(username);
        commodityRateVO.setCreateTime(DateUtil.localDateTimeToString(commodityRate.getCreateTime()));
        commodityRateVO.setUpdateTime(DateUtil.localDateTimeToString(commodityRate.getUpdateTime()));
        return commodityRateVO;
    }

    private CommodityRateListVO pageToVO(Page<CommodityRate> result){
        CommodityRateListVO commodityRateListVO = new CommodityRateListVO();
        commodityRateListVO.setPageNum(result.getCurrent());
        commodityRateListVO.setPageSize(result.getSize());
        commodityRateListVO.setStartRow(1);
        commodityRateListVO.setEndRow(result.getPages());
        commodityRateListVO.setTotal(result.getTotal());
        commodityRateListVO.setPages(result.getPages());
        List<CommodityRate> commodityRateList = result.getRecords();
        List<CommodityRateVO> commodityRateVOList = Lists.newArrayList();
        for(CommodityRate commodityRate: commodityRateList){
            commodityRateVOList.add(this.entityToVO(commodityRate));
        }
        commodityRateListVO.setCommodityRateVOList(commodityRateVOList);
        commodityRateListVO.setFirstPage(1l);
        commodityRateListVO.setPrePage(result.getCurrent() - 1);
        commodityRateListVO.setNextPage(result.getCurrent() + 1);
        commodityRateListVO.setIsFirstPage(result.getCurrent() == 1);
        commodityRateListVO.setIsLastPage(result.getCurrent() == result.getPages());
        commodityRateListVO.setHasPreviousPage(result.getCurrent() > 1);
        commodityRateListVO.setHasNextPage(result.getCurrent()<result.getPages());
        return commodityRateListVO;
    }
}
